using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for RemovePageRow
/// </summary>
public class RemovePageRow
{
    string strDelPageId = "";
    int intpagerowcounttransdoc; // Added by srivatsan
    public RemovePageRow()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public bool RemoveRow(HtmlForm frmObj, DataSet dsePageInfo)
    {
        bool blnStatus = true;
        //string strDelPageId = "";
        int intDelRowId = -1;
        int intPageRowCount = -1;
        int intControlPerRow = -1;

        HtmlInputHidden hihObj = (HtmlInputHidden)frmObj.FindControl("ContentPlaceHolder3").FindControl("hih_DelPageId");//HtmlInputHidden hihObj = (HtmlInputHidden)frmObj.FindControl("hih_DelPageId");
        strDelPageId = hihObj.Value;
        hihObj.Value = "";

        hihObj = (HtmlInputHidden)frmObj.FindControl("ContentPlaceHolder3").FindControl("hih_DelRowId");//hihObj = (HtmlInputHidden)frmObj.FindControl("hih_DelRowId");
        intDelRowId = Convert.ToInt32(hihObj.Value);

        hihObj.Value = "";



        // Added by srivatsan for transaction document to get afrowids for delete on MAy 08 2009
        if (HttpContext.Current.Request.Url.AbsoluteUri.ToLower().Contains("transdoc.aspx"))
        {
            HiddenField hfafrowid = (HiddenField)frmObj.FindControl("ContentPlaceHolder3").FindControl(strDelPageId + "_af_row_id_" + intDelRowId); // Changed on Jan 13 2010
            //TextBox hfafrowid = (TextBox)frmObj.FindControl("ContentPlaceHolder3").FindControl(strDelPageId + "_af_row_id_" + intDelRowId);
            string af_row_id = hfafrowid.Value;
            //string af_row_id = hfafrowid.Text;

            if (af_row_id != "")
            {
                HiddenField hfdelafrowids = (HiddenField)frmObj.FindControl("ContentPlaceHolder3").FindControl("hfdelafrowids");
                string afrowidconcat = hfdelafrowids.Value;

                hfdelafrowids.Value = afrowidconcat + ";" + af_row_id;

            }
        }
        //


        hihObj = (HtmlInputHidden)frmObj.FindControl("ctl00$_sys" + strDelPageId + "_RowCount");//hihObj = (HtmlInputHidden)frmObj.FindControl("_sys" + strDelPageId + "_RowCount");
        intPageRowCount = Convert.ToInt32(hihObj.Value);


        intPageRowCount = Convert.ToInt32(hihObj.Value);
        hihObj.Value = (intPageRowCount - 1).ToString();



        if (HttpContext.Current.Request.Url.ToString().ToLower().Contains("transdoc.aspx"))  // Added by srivatsan
        {
            //HiddenField hfrowcount = (HiddenField)frmObj.FindControl("ctl00$ContentPlaceHolder3$hfrowcount");
            //if (hfrowcount != null)
            //{
                intPageRowCount = Convert.ToInt32(HttpContext.Current.Session["hfrowcount"].ToString());

                intpagerowcounttransdoc = intPageRowCount;

                HttpContext.Current.Session["hfrowcount"]= (intPageRowCount-1).ToString();

               // HttpContext.Current.Session["hfrowcountadd"] = (intPageRowCount - 1).ToString();
            //}
        }

       

        string sAF_ROW_ID = "";
        DataRow[] drr = dsePageInfo.Tables["Page"].Select("page_id='" + strDelPageId.Replace("'", "''") + "'");
        if (drr.Length > 0)
            sAF_ROW_ID = drr[0]["af_row_id"].ToString();
        DataRow[] droPageCtrl = dsePageInfo.Tables["PageCtrl"].Select("page_id='" + sAF_ROW_ID + "'", "control_seq");
        DataRow[] droPage = dsePageInfo.Tables["Page"].Select("page_id='" + strDelPageId.Replace("'", "''") + "'");
        if (droPage[0]["control_per_row"] == DBNull.Value)
        {
            intControlPerRow = GlobalValues.DefaultControlPerRow;
        }
        else
        {
            intControlPerRow = Convert.ToInt32(droPage[0]["control_per_row"].ToString());
        }
        string strPageView = "";
        if (droPage[0]["page_view"] == DBNull.Value)
        {
            strPageView = GlobalValues.DefaultPageView;
        }
        else
        {
            strPageView = droPage[0]["page_view"].ToString();
        }

        blnStatus = ProcessRemoveRow(droPageCtrl, frmObj, strDelPageId, intDelRowId, intPageRowCount, intControlPerRow, strPageView);

        return blnStatus;
    }

    private bool ProcessRemoveRow(DataRow[] droPageCtrl, HtmlForm frmObj, string strDelPageId, int intDelRowId, int intPageRowCount, int intControlPerRow,
        string strPageView)
    {
        bool blnStatus = true;
        blnStatus = RemoveCtrlValues(droPageCtrl, frmObj, strDelPageId, intDelRowId, intPageRowCount);
        if (blnStatus)
        {
            blnStatus = RemoveTableRow(droPageCtrl, frmObj, strDelPageId, intDelRowId, intControlPerRow, strPageView);
        }
        return blnStatus;
    }

    private bool RemoveCtrlValues(DataRow[] droPageCtrl, HtmlForm frmObj, string strDelPageId, int intDelRowId, int intPageRowCount)
    {
        bool blnStatus = true;
        string strCtrlId = "";
        string strCurrentCtrlId = "";
        string strNextCtrlId = "";
        string strPageId = strDelPageId;
        for (int i = intDelRowId; i < intPageRowCount; i++)
        {

            for (int j = 0; j < droPageCtrl.Length; j++)
                {
                    DataRow droCurrent = droPageCtrl[j];
                    strCtrlId = droCurrent["control_attrib_id"].ToString();/////strCtrlId = droCurrent["control_id"].ToString();
                    //strPageId = droCurrent["page_id"].ToString();
                    strCurrentCtrlId = "ctl00$ContentPlaceHolder3$" + strPageId + "_" + strCtrlId + "_" + i.ToString();//strCurrentCtrlId = strPageId + "_" + strCtrlId + "_" + i.ToString();
                    strNextCtrlId = "ctl00$ContentPlaceHolder3$" + strPageId + "_" + strCtrlId + "_" + (i + 1).ToString();//strNextCtrlId = strPageId + "_" + strCtrlId + "_" + (i + 1).ToString();
                    if (frmObj.FindControl(strCurrentCtrlId) != null)
                    {
                        switch (droCurrent["control_html"].ToString().ToUpper())
                        {
                            case "DA":
                            case "DT":
                            //C1WebDateEdit C1txtCurrentObj = (C1WebDateEdit)frmObj.FindControl(strCurrentCtrlId);
                            //C1WebDateEdit C1txtNextObj = (C1WebDateEdit)frmObj.FindControl(strNextCtrlId);
                            //C1txtCurrentObj.Text = C1txtNextObj.Text;
                            //break;
                            case "TB":
                            case "TA":
                                TextBox txtNextObj;
                                if (!Convert.ToBoolean(droCurrent["control_visible"].ToString() == "" ? "false" : droCurrent["control_visible"].ToString()))
                                {
                                    UpdateHiddenFields(strPageId,strCtrlId,strCurrentCtrlId, strNextCtrlId, ref frmObj, intPageRowCount, i);
                                }
                                else
                                {
                                    if (droPageCtrl[j]["control_bind_id"].ToString() != "")
                                    {
                                        HiddenField hfCurrentObj = (HiddenField)frmObj.FindControl(strCurrentCtrlId);
                                        HiddenField hfNextObj = (HiddenField)frmObj.FindControl(strNextCtrlId);
                                        hfCurrentObj.Value = hfNextObj.Value;

                                        strCurrentCtrlId = "ctl00$ContentPlaceHolder3$txt_" + strPageId + "_" + strCtrlId + "_" + i.ToString();
                                        strNextCtrlId = "ctl00$ContentPlaceHolder3$txt_" + strPageId + "_" + strCtrlId + "_" + (i + 1).ToString();
                                        TextBox txtCurrentObj = (TextBox)frmObj.FindControl(strCurrentCtrlId);
                                        txtNextObj = (TextBox)frmObj.FindControl(strNextCtrlId);
                                        txtCurrentObj.Text = txtNextObj.Text;


                                    }
                                    else
                                    {
                                        Control contrl = frmObj.FindControl(strCurrentCtrlId);
                                        if (contrl is HiddenField)
                                        {
                                            HiddenField hfCurrentObj = (HiddenField)frmObj.FindControl(strCurrentCtrlId);
                                            HiddenField hfNextObj = (HiddenField)frmObj.FindControl(strNextCtrlId);
                                            hfCurrentObj.Value = hfNextObj.Value;

                                            strCurrentCtrlId = "ctl00$ContentPlaceHolder3$txt_" + strPageId + "_" + strCtrlId + "_" + i.ToString();
                                            strNextCtrlId = "ctl00$ContentPlaceHolder3$txt_" + strPageId + "_" + strCtrlId + "_" + (i + 1).ToString();
                                            TextBox txtCurrentObj = (TextBox)frmObj.FindControl(strCurrentCtrlId);
                                            txtNextObj = (TextBox)frmObj.FindControl(strNextCtrlId);
                                            txtCurrentObj.Text = txtNextObj.Text;
                                        }
                                        else
                                        {

                                            TextBox txtCurrentObj = (TextBox)frmObj.FindControl(strCurrentCtrlId);
                                            txtNextObj = (TextBox)frmObj.FindControl(strNextCtrlId);
                                            txtCurrentObj.Text = txtNextObj.Text;
                                        }
                                    }
                                    if (i == intPageRowCount - 1)
                                    {
                                        txtNextObj.Text = "";
                                    }
                                }
                                break;
                            case "DD":
                                Control contrl1 = frmObj.FindControl(strCurrentCtrlId);
                                if (!Convert.ToBoolean(droCurrent["control_visible"].ToString() == "" ? "false" : droCurrent["control_visible"].ToString()))
                                {
                                    UpdateHiddenFields(strPageId, strCtrlId, strCurrentCtrlId, strNextCtrlId, ref frmObj, intPageRowCount, i);
                                }
                                else
                                {
                                    if (contrl1 is HiddenField)
                                    {
                                        HiddenField hfCurrentObj = (HiddenField)frmObj.FindControl(strCurrentCtrlId);
                                        HiddenField hfNextObj = (HiddenField)frmObj.FindControl(strNextCtrlId);
                                        hfCurrentObj.Value = hfNextObj.Value;

                                        strCurrentCtrlId = "ctl00$ContentPlaceHolder3$txt_" + strPageId + "_" + strCtrlId + "_" + i.ToString();
                                        strNextCtrlId = "ctl00$ContentPlaceHolder3$txt_" + strPageId + "_" + strCtrlId + "_" + (i + 1).ToString();
                                        TextBox txtCurrentObj = (TextBox)frmObj.FindControl(strCurrentCtrlId);
                                        txtNextObj = (TextBox)frmObj.FindControl(strNextCtrlId);
                                        txtCurrentObj.Text = txtNextObj.Text;
                                    }
                                    else
                                    {
                                        DropDownList ddlCurrentObj = (DropDownList)frmObj.FindControl(strCurrentCtrlId);
                                        DropDownList ddlNextObj = (DropDownList)frmObj.FindControl(strNextCtrlId);
                                        ddlCurrentObj.SelectedValue = ddlNextObj.SelectedValue;
                                        if (i == intPageRowCount - 1)
                                        {
                                            ddlNextObj.SelectedValue = "";
                                        }
                                    }
                                }
                                break;
                            case "CB": //added on 12-Oct-2007
                                if (!Convert.ToBoolean(droCurrent["control_visible"].ToString() == "" ? "false" : droCurrent["control_visible"].ToString()))
                                {
                                    UpdateHiddenFields(strPageId, strCtrlId, strCurrentCtrlId, strNextCtrlId, ref frmObj, intPageRowCount, i);
                                }
                                else
                                {
                                    CheckBox chkCurrentObj = (CheckBox)frmObj.FindControl(strCurrentCtrlId);
                                    CheckBox chkNextObj = (CheckBox)frmObj.FindControl(strNextCtrlId);
                                    chkCurrentObj.Checked = chkNextObj.Checked;
                                    if (i == intPageRowCount - 1)
                                    {
                                        chkNextObj.Checked = false;
                                    }
                                }
                                break;
                        }
                    }
                    //else // Added for Hidden field update
                    //{
                    //    if (HttpContext.Current.Request.Url.ToString().ToLower().Contains("transdoc.aspx"))
                    //    {
                    //        //strCurrentCtrlId = "ctl00$" + strPageId + "_" + strCtrlId + "_" + i.ToString();
                    //        //strNextCtrlId = "ctl00$" + strPageId + "_" + strCtrlId + "_" + (i + 1).ToString();
                    //        strCurrentCtrlId = strPageId + "_" + strCtrlId + "_" + i.ToString();
                    //        strNextCtrlId = strPageId + "_" + strCtrlId + "_" + (i + 1).ToString();
                    //    DynamicDisplayI DDI = new DynamicDisplayI();

                    //    if (DDI.FindControl(frmObj.Controls, strCurrentCtrlId) != null)
                    //    {
                    //        if (DDI.FindControl(frmObj.Controls, strNextCtrlId) != null)
                    //        {
                    //            if (!(droCurrent["control_visible"].ToString() != "" ? Convert.ToBoolean(droCurrent["control_visible"].ToString()) : false))
                    //            {
                    //                HiddenField hfCurrentObj = (HiddenField)DDI.FindControl(frmObj.Controls, strCurrentCtrlId);
                    //                //TextBox hfCurrentObj = (TextBox)DDI.FindControl(frmObj.Controls, strCurrentCtrlId);
                    //                HiddenField hfNextObj = (HiddenField)DDI.FindControl(frmObj.Controls, strNextCtrlId);
                    //                //TextBox hfNextObj = (TextBox)DDI.FindControl(frmObj.Controls, strNextCtrlId);
                    //                hfCurrentObj.Value = hfNextObj.Value;
                    //                //hfCurrentObj.Text = hfNextObj.Text;
                    //                if (i == intPageRowCount - 1)
                    //                {
                    //                    hfNextObj.Value = "";
                    //                    //hfNextObj.Text = "";
                    //                    frmObj.Controls.Remove(hfNextObj);
                    //                    hfNextObj = null;
                    //                }
                    //            }
                    //        }
                    //     }
                    //    }
                    //}
                }
            
        }
        return blnStatus;
    }

    private bool RemoveTableRow(DataRow[] droPageCtrl, HtmlForm frmObj, string strDelPageId, int intDelRowId, int intControlPerRow, string strPageView)
    {
        bool blnStatus = true;
        HtmlTable htaPageTable = null;
        int intValidCtrlCount = 0;
        int intRowsToRemove = -1;
        int intRowCtrlCnt = intControlPerRow;
        htaPageTable = (HtmlTable)frmObj.FindControl("ContentPlaceHolder3").FindControl("tbl_" + strDelPageId);//htaPageTable = (HtmlTable)frmObj.FindControl("tbl_" + strDelPageId);
        //Button btnAddObj = (Button)frmObj.FindControl("btn_" + strDelPageId + "_Add_1");
        for (int i = 0; i < droPageCtrl.Length; i++)
        {
            if (Convert.ToBoolean(droPageCtrl[i]["control_visible"].ToString()) == true)
            {
                if (droPageCtrl[i]["control_html"].ToString().ToUpper() != "HI")
                {
                    if (frmObj.FindControl("ContentPlaceHolder3").FindControl(strDelPageId + "_" + droPageCtrl[i]["control_attrib_id"].ToString() + "_" + intDelRowId.ToString()) != null)//if (frmObj.FindControl(strDelPageId + "_" + droPageCtrl[i]["control_id"].ToString() + "_" + intDelRowId.ToString()) != null)/////j "control_id"
                    {
                        intValidCtrlCount++;
                    }
                }
            }
        }
        intRowsToRemove = intValidCtrlCount / intRowCtrlCnt;
        if (intValidCtrlCount % intRowCtrlCnt >= 1)
        {
            intRowsToRemove = intRowsToRemove + 1;
        }
        for (int i = 0; i < intRowsToRemove; i++)
        {
            if (HttpContext.Current.Request.Url.ToString().ToLower().Contains("transdoc.aspx"))
            {

                htaPageTable = RemoveRowForTransDoc(droPageCtrl, ref frmObj, strDelPageId, intDelRowId, intControlPerRow, strPageView,htaPageTable);

            }
            else
            {
                htaPageTable.Rows.RemoveAt(htaPageTable.Rows.Count - 1);
            }
            
        }
        //htaPageTable.Rows[htaPageTable.Rows.Count - 1].Cells.Count;

        HtmlInputHidden hihObj = (HtmlInputHidden)frmObj.FindControl("ctl00$_sys" + strDelPageId + "_RowCount");//HtmlInputHidden hihObj = (HtmlInputHidden)frmObj.FindControl("_sys" + strDelPageId + "_RowCount");
        // To remove the Remove Button in one to many page, if avilable row is only one
        if (HttpContext.Current.Request.Url.ToString().ToLower().Contains("transdoc.aspx"))
        {
            hihObj.Value = HttpContext.Current.Session["hfrowcount"].ToString();
            if (Convert.ToInt32(HttpContext.Current.Session["hfrowcount"].ToString()) == 1)
            {
                int delcount = 2;
                if (strPageView == "1")
                {
                    if (frmObj.FindControl("ctl00$ContentPlaceHolder3$hfdeladditionalrows") != null) // Added for additional tax fields
                    {
                        HtmlInputHidden htmlhidden = (HtmlInputHidden)frmObj.FindControl("ctl00$ContentPlaceHolder3$hfdeladditionalrows");
                        int addrowcount = Convert.ToInt32(htmlhidden.Value);
                        delcount = delcount + addrowcount;
                    }

                    htaPageTable.Rows[htaPageTable.Rows.Count - delcount].Cells[intRowCtrlCnt + 1].Controls.Clear();
                }
                if (strPageView == "2")
                {
                    htaPageTable.Rows[htaPageTable.Rows.Count - delcount].Cells[(intRowCtrlCnt * 2) + 1].Controls.Clear();
                }
            }
        }
        else
        {

            if (hihObj.Value == "1")
            {
                if (strPageView == "1")
                {
                    htaPageTable.Rows[htaPageTable.Rows.Count - 1].Cells[intRowCtrlCnt + 1].Controls.Clear();
                }
                if (strPageView == "2")
                {
                    htaPageTable.Rows[htaPageTable.Rows.Count - 1].Cells[(intRowCtrlCnt * 2) + 1].Controls.Clear();
                }
            }
        }
        //htaPageTable.Rows[htaPageTable.Rows.Count - 1].Cells[intRowCtrlCnt + 1].Controls.Clear();
        //htaPageTable.Rows[htaPageTable.Rows.Count - 1].Cells[intRowCtrlCnt + 1].Controls.Add(btnAddObj);
        return blnStatus;
    }


    private bool RemoveDefaultFields(DataRow[] droPageCtrl, HtmlForm frmObj, string strDelPageId, int intDelRowId, int intPageRowCount,int removerow)
    {
        bool blnStatus = true;
        string strCtrlId = "";
        string strCurrentCtrlId = "";
        string strNextCtrlId = "";
        string strPageId = strDelPageId;
        for (int i = intDelRowId; i < intPageRowCount; i++)
        {
            strCtrlId = "af_row_id";
            strCurrentCtrlId = strPageId + "_" + strCtrlId + "_" + i.ToString();
            strNextCtrlId = strPageId + "_" + strCtrlId + "_" + (i + 1).ToString();
            if (frmObj.FindControl(strCurrentCtrlId) != null)
            {
                  HiddenField hfafrowidcurrent = (HiddenField)frmObj.FindControl(strCurrentCtrlId);
                  HiddenField hfafrowidnext = (HiddenField)frmObj.FindControl(strNextCtrlId);
                  hfafrowidcurrent.Value = hfafrowidnext.Value;
                  if (i == intPageRowCount - 1)
                  {
                      hfafrowidnext.Value = "";
                  }


                  HiddenField hfcreatedbycurrent = (HiddenField)frmObj.FindControl(strPageId + "_created_by_" + i.ToString());
                  HiddenField hfcreatedbynext = (HiddenField)frmObj.FindControl(strPageId + "_created_by_" + (i + 1).ToString());
                  hfcreatedbycurrent.Value = hfcreatedbynext.Value;
                  if (i == intPageRowCount - 1)
                  {
                      hfcreatedbynext.Value = "";
                  }

                  HiddenField hfmodifiedbycurrent = (HiddenField)frmObj.FindControl(strPageId + "_modified_by_" + i.ToString());
                  HiddenField hfmodifiedbynext = (HiddenField)frmObj.FindControl(strPageId + "_modified_by_" + (i + 1).ToString());
                  hfmodifiedbycurrent.Value = hfmodifiedbynext.Value;
                  if (i == intPageRowCount - 1)
                  {
                      hfmodifiedbynext.Value = "";
                  }

                  HiddenField hfcreatedatcurrent = (HiddenField)frmObj.FindControl(strPageId + "_created_at_" + i.ToString());
                  HiddenField hfcreatedatnext = (HiddenField)frmObj.FindControl(strPageId + "_created_at_" + (i + 1).ToString());
                  hfcreatedatcurrent.Value = hfcreatedatnext.Value;
                  if (i == intPageRowCount - 1)
                  {
                      hfcreatedatnext.Value = "";
                  }

                  HiddenField hfmodifiedatcurrent = (HiddenField)frmObj.FindControl(strPageId + "_modified_at_" + i.ToString());
                  HiddenField hfmodifiedatnext = (HiddenField)frmObj.FindControl(strPageId + "_modified_at_" + (i + 1).ToString());
                  hfmodifiedatcurrent.Value = hfmodifiedatnext.Value;
                  if (i == intPageRowCount - 1)
                  {
                      hfmodifiedatnext.Value = "";
                  }
                }
            

        }


        // af_row_id delete
        try
        {
            string straf_row_id = strDelPageId + "_af_row_id_" + removerow;
            Control con = frmObj.FindControl(straf_row_id);
            if (con != null)
            {
                frmObj.Controls.Remove(con);
                con = null;
                //con.Dispose();
            }


            string strcreated_by = strDelPageId + "_created_by_" + removerow;
            Control con1 = frmObj.FindControl(strcreated_by);
            if (con1 != null)
            {
                frmObj.Controls.Remove(con1);
                con1 = null;
               // con1.Dispose();
            }


            string strmodified_by = strDelPageId + "_modified_by_" + removerow;
            Control con2 = frmObj.FindControl(strmodified_by);
            if (con2 != null)
            {
                frmObj.Controls.Remove(con2);
                con2 = null;
               // con2.Dispose();
            }

            string strcreated_at = strDelPageId + "_created_at_" + removerow;
            Control con3 = frmObj.FindControl(strcreated_at);
            if (con3 != null)
            {
                frmObj.Controls.Remove(con3);
                con3 = null;
                //con3.Dispose();
            }


            string strmodified_at = strDelPageId + "_modified_at_" + removerow;
            Control con4 = frmObj.FindControl(strmodified_at);
            if (con4 != null)
            {
                frmObj.Controls.Remove(con4);
                con4 = null;
                //con3.Dispose();
            }



        }
        catch { }


        return blnStatus;
    }

    private HtmlTable RemoveRowForTransDoc(DataRow[] droPageCtrl,ref HtmlForm frmObj, string strDelPageId, int intDelRowId, int intControlPerRow, string strPageView, HtmlTable htaPageTable)
    {

        bool blnStatus = true;
        int intValidCtrlCount = 0;
        int intRowsToRemove = -1;
        int intRowCtrlCnt = intControlPerRow;
        //htaPageTable = (HtmlTable)frmObj.FindControl("ContentPlaceHolder3").FindControl("tbl_" + strDelPageId);//htaPageTable = (HtmlTable)frmObj.FindControl("tbl_" + strDelPageId);
        int removerow = htaPageTable.Rows.Count - 2;
        if (frmObj.FindControl("ctl00$ContentPlaceHolder3$hfdeladditionalrows") != null)
        {
            HtmlInputHidden htmlhidden = (HtmlInputHidden)frmObj.FindControl("ctl00$ContentPlaceHolder3$hfdeladditionalrows");
            int addrowcount = Convert.ToInt32(htmlhidden.Value);
            removerow = htaPageTable.Rows.Count - (2+addrowcount);
        }

        int intPageRowCount = intpagerowcounttransdoc;


        //RemoveDefaultFields(droPageCtrl, frmObj, strDelPageId, intDelRowId, intPageRowCount, removerow);
        //RemoveHiddenFieldValues(droPageCtrl, ref frmObj, strDelPageId, intDelRowId, intPageRowCount, removerow);

        if (htaPageTable.Rows[removerow].Cells.Count > 0)
        {
            for (int cellcnt = 0; cellcnt < htaPageTable.Rows[removerow].Cells.Count; cellcnt++)
            {
                if (htaPageTable.Rows[removerow].Cells[cellcnt].Controls.Count > 0)
                {
                    for (int cntccount = 0; cntccount < htaPageTable.Rows[removerow].Cells[cellcnt].Controls.Count; cntccount++)
                    {
                        Control control = (Control)htaPageTable.Rows[removerow].Cells[cellcnt].Controls[cntccount];
                        if (control != null)
                        {
                            //htaPageTable.Rows[removerow].Cells[cellcnt].Controls.Clear();
                            if (control is RequiredFieldValidator)
                            {
                                RequiredFieldValidator reqval = (RequiredFieldValidator)control;
                                reqval.Enabled = false;
                                reqval.EnableClientScript = false;
                                reqval.ControlToValidate = "";
                            }
                            if (control is AjaxControlToolkit.ValidatorCalloutExtender)
                            {
                                AjaxControlToolkit.ValidatorCalloutExtender valcal = (AjaxControlToolkit.ValidatorCalloutExtender)control;
                                valcal.Enabled = false;
                                valcal.TargetControlID = "";
                            }
                            //if (control is HiddenField)
                            //{
                            //    HiddenField hf = (HiddenField)control;
                            //    string id = hf.ID;
                            //    string value = hf.Value;

                            //}
                            //htaPageTable.Rows[removerow].Cells[cellcnt].Controls.RemoveAt(cntccount);
                            htaPageTable.Rows[removerow].Cells[cellcnt].Controls.Remove(control);

                            control = null;
                            

                        }
                    }

                    

                   
                    //htaPageTable.Rows[removerow].Cells.RemoveAt(0);
                }
                HtmlTableCell htc = (HtmlTableCell)htaPageTable.Rows[removerow].Cells[cellcnt];
                htaPageTable.Rows[removerow].Cells.Remove(htc);
                //htc = null;
                //htaPageTable.Rows[removerow].Cells.RemoveAt(cellcnt);
                cellcnt = cellcnt - 1;
            }
        }

        //if (htaPageTable.Rows[removerow].Cells.Count > 0)
        //{
        //    foreach (HtmlTableCell htc in htaPageTable.Rows[removerow].Cells)
        //    {
        //        if (htc.Controls.Count > 0)
        //        {
        //            foreach (Control cntrl in htc.Controls)
        //            {
        //                if (cntrl.Controls.Count > 0)
        //                {
        //                    foreach (Control cntrl1 in cntrl.Controls)
        //                    {
        //                        if (cntrl1.Controls.Count > 0)
        //                        {
        //                            foreach (Control cntrl2 in cntrl1.Controls)
        //                            {
        //                                cntrl1.Controls.Remove(cntrl2);
        //                            }
        //                        }
        //                    }
        //                }

        //                htc.Controls.Remove(cntrl);
        //            }
        //        }

        //        htaPageTable.Rows[removerow].Cells.Remove(htc);
        //    }
        //}

        htaPageTable.Rows.RemoveAt(removerow);
        return htaPageTable;

    }

    private void UpdateHiddenFields(string strPageId,string strCtrlId, string strCurrentCtrlId, string strNextCtrlId,ref HtmlForm frmObj,int intPageRowCount,int i)
    {
        try
        {
            if (HttpContext.Current.Request.Url.ToString().ToLower().Contains("transdoc.aspx"))
            {
                strCurrentCtrlId = strPageId + "_" + strCtrlId + "_" + i.ToString();
                strNextCtrlId = strPageId + "_" + strCtrlId + "_" + (i + 1).ToString();
                DynamicDisplayI DDI = new DynamicDisplayI();

                if (DDI.FindControl(frmObj.Controls, strCurrentCtrlId) != null)
                    if (DDI.FindControl(frmObj.Controls, strNextCtrlId) != null)
                    {
                        HiddenField hfCurrentObj = (HiddenField)DDI.FindControl(frmObj.Controls, strCurrentCtrlId);
                        //TextBox hfCurrentObj = (TextBox)DDI.FindControl(frmObj.Controls, strCurrentCtrlId);
                        HiddenField hfNextObj = (HiddenField)DDI.FindControl(frmObj.Controls, strNextCtrlId);
                        //TextBox hfNextObj = (TextBox)DDI.FindControl(frmObj.Controls, strNextCtrlId);
                        hfCurrentObj.Value = hfNextObj.Value;
                        //hfCurrentObj.Text = hfNextObj.Text;
                        if (i == intPageRowCount - 1)
                        {
                            hfNextObj.Value = "";
                            //hfNextObj.Text = "";
                            frmObj.Controls.Remove(hfNextObj);
                            hfNextObj = null;
                        }
                    }
            }
        }
        catch
        {
        }
    }

    // This will set the values of the Hidden fields and Remove the hidden field for removed row
    private void RemoveHiddenFieldValues(DataRow[] droPageCtrl,ref HtmlForm frmObj, string strDelPageId, int intDelRowId, int intPageRowCount, int removerow)
    {
        try
        {
            bool blnStatus = true;
            string strCtrlId = "";
            string strCurrentCtrlId = "";
            string strNextCtrlId = "";
            string strPageId = strDelPageId;
            for (int i = intDelRowId; i < intPageRowCount; i++)
            {
                foreach (DataRow dr in droPageCtrl)
                {
                    if (!(dr["control_visible"].ToString() != "" ? Convert.ToBoolean(dr["control_visible"].ToString()) : false))
                    {
                        strCtrlId = dr["control_id"].ToString();
                        strCurrentCtrlId = strPageId + "_" + strCtrlId + "_" + i.ToString();
                        strNextCtrlId = strPageId + "_" + strCtrlId + "_" + (i + 1).ToString();
                        if (frmObj.FindControl(strCurrentCtrlId) != null)
                        {
                            if (frmObj.FindControl(strNextCtrlId) != null)
                            {
                                HiddenField hfafrowidcurrent = (HiddenField)frmObj.FindControl(strCurrentCtrlId);
                                HiddenField hfafrowidnext = (HiddenField)frmObj.FindControl(strNextCtrlId);
                                hfafrowidcurrent.Value = hfafrowidnext.Value;
                                if (i == intPageRowCount - 1)
                                {
                                    hfafrowidnext.Value = "";
                                }
                            }
                        }

                        string strctrlid = strDelPageId + "_" + strCtrlId + "_" + removerow;
                        Control con = frmObj.FindControl(strctrlid);
                        if (con != null)
                        {
                            frmObj.Controls.Remove(con);
                            con = null;
                            //con.Dispose();
                        }
                    }
                }


            }
        }
        catch(Exception ex)
        {
            LogWriter.WriteLog("Error : RemoveHiddenFieldValues :"+ ex.Message);
        }
    }
    
}

